<?php
include '../template.php';
include '../dbconfig/config.php';
include '../dbconfig/opendb.php';
include './peptideformat.php';
include '../util/dbutil.php';
getTopWithJS("..");
?>

&nbsp;<br />
	
<?php
    if(!isset($_GET['id'])) {
            die('Phosphosite information not available!');
    }

    $id = $_GET['id'];

    $ref = $_GET['ref'];
    $getref = "";
    if (!empty($ref)) {
        $getref = "&ref=$ref";
    }

    $nsurr = 10;

//    foreach(array_unique(explode(",",$ids)) as $id) {
	// query database for phosphorylation sites
        $query  = "SELECT site.protein AS pid, protein.annotation, protein.sequence, organism.latin, organism.commonName, site.locationInProtein AS loc ".
                  "FROM site,protein,organism ".
                  "WHERE site.protein=protein.id AND protein.organism=organism.label AND site.ptm='p' AND site.id=$id;";
        $result = mysql_query($query) or die('Error, query failed: '.$query);
	
	echo "<table>";
	while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
		echo "<tr><td align=\"right\"><b>Organism:</b></td><td>&nbsp;</td><td><i>{$row['latin']}</i> ({$row['commonName']})</td></tr>";

                $pro = $row['pid'];
		echo "<tr><td align=\"right\"><b>Protein:</b></td><td>&nbsp;</td><td><a href=\"protein.php?id=".$pro.$getref."\">".implode("&nbsp;", getXref($row['pid'])).": ".$row['annotation']."</a></td></tr>";
		$loc = $row['loc'];
		echo "<tr><td align=\"right\"><b>Position:</b></td><td>&nbsp;</td><td>".($loc+1)."</td></tr>";
		$sequence = $row['sequence'];
		$start = max(0,$loc-$nsurr);
		$end = min(strlen($sequence)-1,$loc+$nsurr);
		$surr = substr($sequence,$start,$loc-$start)."<font color=\"red\">".substr($sequence,$loc,1)."</font>".substr($sequence,$loc+1,$end-$loc);
		echo "<tr><td align=\"right\"><b>Surrounding sequence:</b></td><td>&nbsp;</td><td>{$surr}</td></tr>";
	}

        // query spectral count
        $query = "SELECT DISTINCT mass ".
                 "FROM peptide, peptideSiteRelation ".
                 "WHERE peptide.id=peptideSiteRelation.peptide AND peptideSiteRelation.site=$id";
        if (!empty($ref)) {
            $query .= " AND '$ref'=(SELECT mass.dataSource FROM mass WHERE mass.id=peptide.mass)";
        }
        $result = mysql_query($query) or die("Error, query failed: $query");
        while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
            $mass[] = $row['mass'];
        }
//        $massLink = "<a href=\"./spectrum.php?id=".implode(',',$mass)."\">".count($mass)."</a>&nbsp;&nbsp;";
        $massLink = count($mass);
        echo "<tr><td align=\"right\" width=\"100\"><b>No. of Spectra:</b></td><td>&nbsp;</td><td>$massLink</td></tr>";

        // query dataSource
        $query = "SELECT dataSource.id,dataSource.reference FROM dataSource,siteDataSourceRelation WHERE siteDataSourceRelation.site='$id' AND siteDataSourceRelation.dataSource=dataSource.id ORDER BY pubmed DESC";
        $result = mysql_query($query) or die('Error, query failed');
        while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
                $dataSource[$row['id']] = $row['reference'];
        }
        if (count($dataSource)>1) {
            $dataSource[0] = "All";
            ksort($dataSource);
        }

        echo "<tr><td align=\"right\"><b>Reference:</b></td><td>&nbsp;</td><td>";
        echo "<form method=\"get\" action=\"phosphosite.php\">";
        echo "<input name=\"id\" type=\"hidden\" id=\"id\" value=\"$id\" />";
        echo "<select name=\"ref\" size=\"1\">";
        foreach($dataSource as $dsId => $dsRef) {
                echo "<option value=\"$dsId\"";
                if ($ref==$dsId) {
                    echo "selected=\"selected\"";
                }
                echo ">$dsRef</option>";
        }
        echo "</select>";
        echo "<input name=\"select\" type=\"submit\" id=\"select\" value=\"Select\" /></form>";
        echo "</td></tr>";

        echo "<tr><td valign=\"top\" align=\"right\"><b>NR Peptides:</b><td>&nbsp;</td></td><td>";
	// query database for peptide
        if (empty($ref))
            $query = "SELECT DISTINCT peptide.phosphoSequence, peptideSiteRelation.locationInPeptide, peptide.location ".
                     "FROM peptideSiteRelation,peptide ".
                     "WHERE peptideSiteRelation.site=$id AND peptideSiteRelation.peptide=peptide.id;";
        else
            $query = "SELECT DISTINCT peptide.phosphoSequence, peptideSiteRelation.locationInPeptide, peptide.location ".
                     "FROM peptideSiteRelation,peptide,mass ".
                     "WHERE peptideSiteRelation.site=$id AND peptideSiteRelation.peptide=peptide.id AND peptide.mass=mass.id AND mass.dataSource=$ref;";
//        echo $query;
        $result = mysql_query($query) or die('Error, query failed'.$query);
        while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
            $nrseq = $row['phosphoSequence'];
            $linkformat = str_replace('#', '%23', $nrseq);
//            echo $nrseq;
            $formated = format_nr_pep_unlink_exclusiveself($nrseq,'.',$row['locationInPeptide'],$getref);
            echo "<a href=\"../data/peptide.php?pnrseq=$linkformat&pro=$pro&loc={$row['location']}$getref\">$formated</a><br/>";
        }
        echo "</td></tr>";
	echo "</table>";

//        echo "<hr/>";
//    }
	include '../dbconfig/closedb.php';
?>
	

<?php getBottom(); ?>
